package com.tusuo.stds.ui.complete;

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.Constants;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.ApiResult;
import com.tusuo.stds.rest.CompleteQuestion;
import com.tusuo.stds.util.DESedeUtil;
import com.tusuo.stds.util.Utils;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;

/**
 * Created by developer on 17-9-25.
 */

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(CompleteQuestionPresenter.class)
public class CompleteQuestionPresenter extends MvpBasePresenter<CompleteQuestionView> {

    @Inject
    ApiHelper mApiHelper;
    @Inject
    SharedPreferences mSharedPref;

    private Activity mActivity;
    private boolean mFinalQuestion;
    private SubmitCompleteQuestionSubscriber mSubmitAnswerSubscriber;


    CompleteQuestionPresenter(Activity activity, int dataId, int type, int completeId) {
        mActivity = activity;
        Injector.initCompleteQuestionPresenterComponent().inject(this);
        mApiHelper.getCompleteQuestion(new GetCompleteQuestionSubscriber(),dataId,type,completeId);
    }

    public void submitAnswer(String answerContent,int dataId,long elapseSec,boolean isFinal) {
        mFinalQuestion = isFinal;
        mSubmitAnswerSubscriber = new SubmitCompleteQuestionSubscriber(answerContent,dataId, elapseSec, isFinal);
        mApiHelper.submitCompleteQuestion(mSubmitAnswerSubscriber,dataId,(int)elapseSec,answerContent);
    }

    @Override
    public void attachView(CompleteQuestionView view) {
        super.attachView(view);
        if (mSharedPref == null) {
            mSharedPref = PreferenceManager.getDefaultSharedPreferences(mActivity);
        }
        getView().setSecretKey(DESedeUtil.getSecretKey(mSharedPref.getString(Constants.KEY_COOKIE, "")));
    }

    private class SubmitCompleteQuestionSubscriber extends Subscriber<ApiResult> {
        private int questionId;
        private long elapseSec;
        private boolean isFinal;
        private String answerContent;


        SubmitCompleteQuestionSubscriber(String answerContent, int dataId, long elapseSec, boolean isFinal) {
            this.questionId = dataId;
            this.elapseSec =elapseSec;
            this.answerContent = answerContent;
            this.isFinal = isFinal;
        }

        @Override
        public void onCompleted() {
            if (mFinalQuestion) {
                if (getView() != null) getView().toResultActivity();
            }
        }

        @Override
        public void onError(Throwable e) {
            Utils.UI.showToastShort("提交答案失败！请联系管理员或稍候再试");
            if (getView() != null) {
                getView().requestSubmitQuestion(questionId,elapseSec,isFinal,answerContent);
            }
        }

        @Override
        public void onNext(ApiResult apiResult) {
            if (getView() != null) {
                getView().toNextQuestion();
            }

        }
    }

    private class GetCompleteQuestionSubscriber extends Subscriber<CompleteQuestion> {

        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            Utils.UI.showToastShort("网络连接失败！");
        }

        @Override
        public void onNext(CompleteQuestion completeQuestion) {
            if (getView() != null && completeQuestion != null) {
                if (completeQuestion.QuestionCount > 0) {
                    getView().initQuestion(completeQuestion.QuestionList, completeQuestion.Id);
                }
            }
        }
    }

}
